package service

import (
	"context"
	"insighttracking/query/dao"
	"insighttracking/query/model"
)

type _enterpriseCustomerService struct{}

func (s _enterpriseCustomerService) GetByIdApi(ctx context.Context, id int64) (*model.EnterpriseCustomerEntity, error) {
	return dao.EnterpriseCustomerEntity.WithContext(ctx).Where(dao.EnterpriseCustomerEntity.ID.Eq(id)).First()
}

func (s _enterpriseCustomerService) GetByNameApi(ctx context.Context, enterpriseId int64, name string) (*model.EnterpriseCustomerEntity, error) {
	return dao.EnterpriseCustomerEntity.WithContext(ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(enterpriseId),
		dao.EnterpriseCustomerEntity.Name.Eq(name),
	).First()
}

func (s _enterpriseCustomerService) CreateApi(ctx context.Context, tx *dao.QueryTx, entity *model.EnterpriseCustomerEntity) error {
	if err := tx.EnterpriseCustomerEntity.WithContext(ctx).Create(entity); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) Create2Api(ctx context.Context, entity *model.EnterpriseCustomerEntity) error {
	if err := dao.EnterpriseCustomerEntity.WithContext(ctx).Create(entity); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) DeleteByIdApi(ctx context.Context, tx *dao.QueryTx, enterpriseId int64, id int64) error {
	if _, err := tx.EnterpriseCustomerEntity.WithContext(ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(enterpriseId),
		dao.EnterpriseCustomerEntity.ID.Eq(id),
	).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) DeleteByEnterpriseIdApi(ctx context.Context, tx *dao.QueryTx, enterpriseId int64) error {
	if _, err := tx.EnterpriseCustomerEntity.WithContext(ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(enterpriseId),
	).Delete(); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) ModifyStatusByEnterpriseIdApi(ctx context.Context, tx *dao.QueryTx, enterpriseId, status int64) error {
	if _, err := tx.EnterpriseCustomerEntity.WithContext(ctx).Where(
		dao.EnterpriseCustomerEntity.EnterpriseID.Eq(enterpriseId),
	).UpdateSimple(dao.EnterpriseCustomerEntity.Status.Value(status)); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) ModifyStatusByIdApi(ctx context.Context, tx *dao.QueryTx, id, status int64) error {
	if _, err := tx.EnterpriseCustomerEntity.WithContext(ctx).Where(
		dao.EnterpriseCustomerEntity.ID.Eq(id),
	).UpdateSimple(dao.EnterpriseCustomerEntity.Status.Value(status)); err != nil {
		return err
	}
	return nil
}

func (s _enterpriseCustomerService) GetNameByIdApi(ctx context.Context, id int64) (name string, err error) {
	err = dao.EnterpriseCustomerEntity.WithContext(ctx).Where(dao.EnterpriseCustomerEntity.ID.Eq(id)).Pluck(dao.EnterpriseCustomerEntity.Name, &name)
	return
}

var EnterpriseCustomerService = new(_enterpriseCustomerService)
